// import { createReducer, createAction } from "@reduxjs/toolkit";

// const schoolReducer = (state = {
//     name: '江苏第二师范',
//     address: '宿迁市小行路101号'
// }, action) => {
//     switch (action.type) {
//         case 'setSchoolName':
//             return {
//                 ...state,
//                 name: action.payload
//             }
//         case 'setSchoolAddress':
//             return {
//                 ...state,
//                 address: action.payload
//
//             }
//         default:
//             return state
//     }
// }

// // 创建action
// export const setSchoolName = createAction('setSchoolName')
// export const setSchoolAddress = createAction('setSchoolAddress')
// export const setSchool = createAction('setSchool')
//
// // 创建reducer
// const schoolReducer = createReducer({
//     name: '江苏第二师范',
//     address: '宿迁市小行路101号'
// }, builder => {
//     builder.addCase(setSchoolName,(state,action) => {
//         state.name = action.payload
//     })
//     builder.addCase(setSchoolAddress,(state,action) => {
//         state.address = action.payload
//     })
//     builder.addCase(setSchool,(state,action) => {
//         for (const key in action.payload) {
//             state[key] = action.payload[key]
//         }
//     })
// })
// export default schoolReducer

import {createSlice} from "@reduxjs/toolkit";
// 创建一个全自动的reducer切片对象(它会自动帮我们创建action和reducer,并且将他们绑定)
const schoolSlice = createSlice({
    // 名称
    name: "school",
    // 初始化数据
    initialState: {
        name: '江苏第二师范',
        address: '宿迁市小行路101号',
        obj: {}
    },
    // 定义整合器
    reducers: {
        setSchoolName(state,action){
            state.name = action.payload
        },
        setSchoolAddress(state,action){
            state.address = action.payload
        },
        setSchool(state,action){
            for (const key in action.payload) {
                state[key] = action.payload[key]
            }
        },
        setObj(state,action){
            for (const key in action.payload){
                state.obj[key] = action.payload[key]
            }
        }
    }
})
// createSlice返回的并不是一个reducer对象(切片对象)。

// 导出school切片对象(切片对象：就是将action和reducer进行自动整合的对象)
export default schoolSlice
